package com.example.myapplication.tack_10;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myapplication.R;
import com.example.myapplication.utils.Common;
import com.example.myapplication.utils.OkHttpTool;
import com.example.myapplication.utils.StateData;
import com.google.gson.JsonObject;

import org.json.JSONException;
import org.json.JSONObject;

import java.nio.file.Files;
import java.nio.file.Paths;

public class NewRequestActivity extends AppCompatActivity {
//    <!--    {
//"appealCategoryId": 5,
//"title": "测试诉求 001",
//"content": "诉求内容诉求内容诉求内容诉求内容诉求内容",
//"undertaker": "大连市政府",
//"imgUrl": "/profile/abc.png"
//}-->
    private ImageView exit ;
    private EditText title;
    private EditText content;
    private EditText appealCategoryId;
    private EditText under;
    private TextView submit;
    private JSONObject jsonObject;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tack_10_activity_new_request);
        init();
        Intent intent = getIntent();
        String id = intent.getStringExtra(Common.COMMON);
        exit_();
        submit_();
    }

    private void submit_() {

        submit.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.O)
            @Override
            public void onClick(View v) {
                String id = appealCategoryId.getText().toString();
                String tlt = title.getText().toString();
                String cont = content.getText().toString();
                String und = under.getText().toString();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        // 读取图片文件
                        try {
                            jsonObject.put("appealCategoryId",id);
                            jsonObject.put("title",tlt);
                            jsonObject.put("content",cont);
                            jsonObject.put("undertaker",und);
                        } catch (Exception e) {
                            throw new RuntimeException(e);
                        }
                        String json = jsonObject.toString();
                        innovate(json);
                    }
                });


            }
        });
    }

    private void innovate(String json) {
        String url = "prod-api/api/gov-service-hotline/appeal";
        OkHttpTool.doNetRequest(url, OkHttpTool.POST, StateData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                StateData stateData = (StateData)obj;
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if(stateData.getCode() == 200){
                            Toast.makeText(NewRequestActivity.this,stateData.getMsg(),Toast.LENGTH_SHORT).show();
                        }else{
                            Toast.makeText(NewRequestActivity.this,stateData.getMsg(),Toast.LENGTH_SHORT).show();
                        }
                    }
                });

            }
            @Override
            public void onFailure(String msg) {

            }
        },json);
    }

    private void exit_() {
        exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NewRequestActivity.this.finish();
            }
        });
    }

    private void init() {
        exit = findViewById(R.id.exit);
        title = findViewById(R.id.title);
        content = findViewById(R.id.content);
        under = findViewById(R.id.under);
        appealCategoryId = findViewById(R.id.appealCategoryId);
//        imags = findViewById(R.id.imags);
        submit = findViewById(R.id.submit);
        jsonObject = new JSONObject();
    }
}